#include "DataProcess.h"
#include "common\def.h"
#include <comLib\net.h>
#include <QSettings>
#include "MsgCenter.h"


DDataProcess::DDataProcess(QObject *parent)
{
	m_RecvPacket.buffer = new QByteArray;
	m_iHeadSize = sizeof(HeadInfo);
	m_pMsgCenter = DMsgCenter::getInstance();

	m_mousePressMap[Qt::MouseButton::LeftButton] = MOUSEEVENTF_LEFTDOWN;
	m_mousePressMap[Qt::MouseButton::RightButton] = MOUSEEVENTF_RIGHTDOWN;

	m_mouseReleaseMap[Qt::MouseButton::LeftButton] = MOUSEEVENTF_LEFTUP;
	m_mouseReleaseMap[Qt::MouseButton::RightButton] = MOUSEEVENTF_RIGHTUP;

	m_MsgProcess[SERVER_MSG::SERVER_MOUSE_INFO] = std::bind(&DDataProcess::ProcessMouseInfo, this, std::placeholders::_1, std::placeholders::_2);
}


DDataProcess::~DDataProcess()
{
	delete m_RecvPacket.buffer;
}

EnHandleResult DDataProcess::OnHandShake(ITcpClient* pSender, CONNID dwConnID)
{
	return EnHandleResult::HR_OK;
}

EnHandleResult DDataProcess::OnReceive(ITcpClient* pSender, CONNID dwConnID, const BYTE* pData, int iLength)
{
	m_RecvPacket.buffer->append((const char *)pData, iLength);

	do{
		if (m_RecvPacket.bCompleteHead == false){

			memcpy(&m_RecvPacket.headInfo, m_RecvPacket.buffer->data(), m_iHeadSize);
			m_RecvPacket.buffer->remove(0, m_iHeadSize);
			m_RecvPacket.bCompleteHead = true;
		}
		if (m_RecvPacket.bCompleteHead && m_RecvPacket.headInfo.bodySize <= m_RecvPacket.buffer->size()){

			run(m_RecvPacket.headInfo, m_RecvPacket.buffer->data());
			m_RecvPacket.buffer->remove(0, m_RecvPacket.headInfo.bodySize);

			m_RecvPacket.bCompleteHead = false;
		}
	} while (m_RecvPacket.buffer->size() >= m_iHeadSize && m_RecvPacket.bCompleteHead == false);

	return EnHandleResult::HR_OK;
}

EnHandleResult DDataProcess::OnSend(ITcpClient* pSender, CONNID dwConnID, const BYTE* pData, int iLength)
{
	return __super::OnSend(pSender, dwConnID, pData, iLength);
}

EnHandleResult DDataProcess::OnClose(ITcpClient* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode)
{
	return EnHandleResult::HR_OK;
}

void DDataProcess::run(const HeadInfo &headInfo, const void *data)
{
	auto findRet = m_MsgProcess.find((SERVER_MSG)headInfo.msgType);
	if (findRet != m_MsgProcess.end())
	{
		findRet->second(headInfo, data);
	}
	else
	{
		assert(false);
	}
}

bool DDataProcess::ProcessMouseInfo(const HeadInfo headInfo, const void *data)
{
	MouseCmd mouseCmd;
	memcpy(&mouseCmd, data, headInfo.bodySize);

	POINT point;
	point.x = mouseCmd.mousePos.x;
	point.y = mouseCmd.mousePos.y;

	switch (mouseCmd.actionInfo)
	{
	case MouseActionInfo::IDLE_ACTION:
	{
		::SetCursorPos(mouseCmd.mousePos.x, mouseCmd.mousePos.y);
		::SetCapture(WindowFromPoint(point));
	}
		break;
	case MouseActionInfo::PRESS:
	{
		mouse_event(m_mousePressMap[(Qt::MouseButton)mouseCmd.btnInfo], 0, 0, 0, 0);
	}
	break;
	case MouseActionInfo::RELEASE:
	{
		mouse_event(m_mouseReleaseMap[(Qt::MouseButton)mouseCmd.btnInfo], 0, 0, 0, 0);
	}
	break;
	default:
		break;
	}

	return true;
}

DNetClient::DNetClient() :
m_pDataProcess(new DDataProcess())
{
	QSettings setFile("client.ini", QSettings::IniFormat);
	m_BindIp = setFile.value("serverIp", QStringLiteral("")).toString();

	if (m_BindIp.isEmpty())
	{
		setFile.setValue("serverIp", QStringLiteral("192.168.123.8"));
		m_BindIp = QStringLiteral("192.168.123.8");
	}
	m_pTcpClient = HP_Create_TcpClient(m_pDataProcess.get());
}

DNetClient::~DNetClient()
{
	m_pTcpClient->Stop();
	HP_Destroy_TcpClient(m_pTcpClient);
}

bool DNetClient::connectServer()
{
	if (!m_pTcpClient->Start((LPCTSTR)m_BindIp.toUtf8().data(), 8888)){
		DWORD ret = GetLastError();
		return false;
	}

	return true;
}

void DNetClient::sendPacket(HeadInfo headInfo, const char *data)
{
	m_pTcpClient->Send((BYTE *)&headInfo, sizeof(HeadInfo));
	m_pTcpClient->Send((BYTE *)data, headInfo.bodySize);
}